﻿using MinecraftClient;
using MinecraftClient.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace MinecraftClient.ChatBots.MyBots
{ 
    public class MinerBot : ChatBot 
    {
        // Define variables
        private Location targetLocation;
        private bool enable = false;
        private Material targetBlock = Material.Cobblestone;

        // General overwrite methods.

        public override void Initialize()
        {
            var x = GetCurrentLocation().X;
            var y = GetCurrentLocation().Y + 2;
            var z = GetCurrentLocation().Z;
            targetLocation = new Location(x, y, z);
            LogToConsole("MinerBot is ready!");
        }
        
        public override void Update() 
        {
            if (!enable) return;
            Material actualBlock = GetWorld().GetBlock(targetLocation).Type;
            if (actualBlock.Equals(targetBlock)) DigBlock(targetLocation);
        }

        public override void GetText(string text)
        {
            text = GetVerbatim(text);
            Regex cmd = new Regex(@"^\[.+\]<Gare_TH> minerbot set (\d) (\d) (\d)$");
            Regex en = new Regex(@"^\[.+\]<Gare_TH> minerbot off$");
            Regex help = new Regex(@"^\[.+\]<(.+)> minerbot help$");
            Regex toggle = new Regex(@"^\[.+\]<Gare_TH> minerbot toggle$");
            Regex block = new Regex(@"^\[.+\]<Gare_TH> minerbot block$");
            if (help.IsMatch(text))
            {
                SendText("minerbot set <x> <y> <z> | off | toggle | block");
                return;
            }

            if (toggle.IsMatch(text))
            {
                Toggle();
                return;
            }

            if (block.IsMatch(text))
            {
                SetBlock();
                return;
            }

            if (en.IsMatch(text))
            {
                enable = false;
                UnloadBot();
                SendText("Unloaded.");
                return;
            }

            if (cmd.IsMatch(text))
            {
                var x = int.Parse(cmd.Match(text).Groups[1].Value);
                var y = int.Parse(cmd.Match(text).Groups[2].Value);
                var z = int.Parse(cmd.Match(text).Groups[3].Value);
                SetLocation(x, y, z);
            }
        }

        private void SetBlock()
        {
            Material b = GetWorld().GetBlock(targetLocation).Type;
            targetBlock = b;
            SendText(String.Format("Set target block type {0} at ({1})", b, targetLocation.ToString()));
        }

        private void Toggle()
        {
            SendText(enable ? "Now is stopping." : "Now is running.");
            enable = !enable;
        }

        private void SetLocation(int x, int y, int z)
        {
            ChangeSlot(0);
            targetLocation = new Location(x, y, z);
            SendText(String.Format("Set target block coordinate ({0}, {1}, {2})", x, y, z));
        }

    }
}